﻿using Hardcodet.Wpf.TaskbarNotification;
using Lib.WPF.Controls;
using Lib.WPF.Core;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Lib.WPF; 
/// <summary>
/// 入口程序
/// </summary>
public partial class Main : Window {
    /// <summary>
    /// 
    /// </summary>
    private ViewModels.Main VM { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public Main() {

        InitializeComponent();

        VM = new ViewModels.Main();

        DataContext = VM;

        CommandBindings.Add(new CommandBinding(SystemCommands.MinimizeWindowCommand, (Sender, e) => {
            WindowState = WindowState.Minimized;
        }));
        CommandBindings.Add(new CommandBinding(SystemCommands.MaximizeWindowCommand, (Sender, e) => {
            WindowState = WindowState.Maximized;
        }));
        CommandBindings.Add(new CommandBinding(SystemCommands.RestoreWindowCommand, (Sender, e) => {
            WindowState = WindowState.Normal;
        }));
        CommandBindings.Add(new CommandBinding(SystemCommands.CloseWindowCommand, (Sender, e) => {
            if (VM.IsCloseMinTray) {
                e.Handled = true; this.Hide();
            } else {
                Application.Current?.Shutdown();
            }
        }, (Sender, e) => {
            e.CanExecute = e.Handled = true;
        }));
        //
        this.Focus();
    }
    /// <summary>
    /// 
    /// </summary>
    protected override void OnClosing(CancelEventArgs e) {
        if (VM.IsCloseMinTray) {
            if (!ViewModels.Main.ApplicationClosing) {
                e.Cancel = true; this.Hide();
            }
        }
        base.OnClosing(e);
    }
    /// <summary>
    /// 
    /// </summary>
    private void StackPanel_ContextMenuOpening(object Sender, ContextMenuEventArgs e) {
        if (Sender is not FrameworkElement Element) {
            return;
        }
        var ContextMenu = Element.ContextMenu;
        //
        if (e.Source is RadioButton Radio) {
            VM.SelectedTab = Radio;
            var Count = VM.Tabs.Children.Count;
            var Index = VM.Tabs.Children.IndexOf(Radio);
            if (Index == 0) {
                foreach (var Item in ContextMenu.Items) {
                    if (Item is MenuItem menuItem) {
                        if (menuItem.Name == "CloseTabFront") {
                            menuItem.IsEnabled = false;
                        } else {
                            menuItem.IsEnabled = true;
                        }
                    }
                }
            } else if (Index == (Count - 1)) {
                foreach (var Item in ContextMenu.Items) {
                    if (Item is MenuItem menuItem) {
                        if (menuItem.Name == "CloseTabBack") {
                            menuItem.IsEnabled = false;
                        } else {
                            menuItem.IsEnabled = true;
                        }
                    }
                }
            } else {
                foreach (var Item in ContextMenu.Items) {
                    if (Item is MenuItem menuItem) {
                        menuItem.IsEnabled = true;
                    }
                }
            }
        }
    }
}